package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.RoomBean;
import com.project.bean.StudentBean;
import com.project.mapper.IRoomMapper;
import com.project.mapper.IStudentMapper;
import com.project.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class RoomService implements IRoomService {

    @Autowired
    private IRoomMapper roomMapper;

    @Autowired
    private IStudentMapper studentMapper;
    @Override
    public List<RoomBean> findSexRoom(String sex) {
        return roomMapper.findSexRoom(sex);
    }

    @Override
    public IPage<RoomBean> findRooms(String address, String status, String canLive, String type, Integer pageNum) {
        return roomMapper.findRooms(new Page<>(pageNum,2),address,status,canLive,type);
    }

    @Override
    public void addRoom(RoomBean roomBean) {
        roomMapper.insert(roomBean);
    }

    @Override
    public RoomBean findRoomById(Integer roomId) {
        return roomMapper.findRoomById(roomId);
    }

    @Override
    public boolean deleteRoom(Integer roomId) {
        QueryWrapper<StudentBean> wrapper = new QueryWrapper<>();
        wrapper.eq("fk_roomId",roomId);
        Integer count = studentMapper.selectCount(wrapper);
        if (count == 0) {
                roomMapper.deleteById(roomId);
                return true;
        }
        return false;
    }
}
